package com.ahp.action;

import java.io.IOException;
import java.io.PrintWriter;

import com.ahp.bean.User;
import com.ahp.service.UserService;
import com.ahp.util.FreemarkerTest;
import com.ahp.util.PageList;
import com.ahp.util.Pages;

public class UserAction extends CommonActionSupport {
	private static final long serialVersionUID = 1L;
	private User user;
	private UserService userService;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserService getUserService() {
		return userService;
	}

	/**
	 * @return
	 * @date:Sep 7, 2009
	 * @author: 李晓娜
	 */
	public String saveUser() {
		userService.saveUser(user);
		return SUCCESS;
	}

	/**
	 * @return
	 * @date:Sep 7, 2009
	 * @author: 李晓娜
	 */
	public String updateUser() {
		userService.updateUser(user);
		return SUCCESS;
	}

	/**
	 * @return
	 * @date:Sep 7, 2009
	 * @author: 李晓娜
	 */
	public String getOneUser() {
		user = userService.getUser(user.getId());
		return SUCCESS;
	}

	/**
	 * @return
	 * @date:Sep 7, 2009
	 * @author: 李晓娜
	 */
	public String login() {
		boolean result = userService.login(user.getLoginName(), user.getPassword());
		new FreemarkerTest().createHTML();
		if (result) {
			return SUCCESS;
		} else {
			return "failed";
		}
	}

	/**
	 * @return
	 * @date:Sep 7, 2009
	 * @author: 李晓娜
	 */
	public String listUser() {
		Pages pages = new Pages("listUser.action?method=listUser", getRequest());
		PageList pagelist = userService.listUser(pages);
		getRequest().setAttribute("list", pagelist.getObjectList());
		getRequest().setAttribute("pageStr", pagelist.getPageStr());
		return SUCCESS;
	}
	
	public String checkUser(){
		boolean result = userService.checkUser(user);
		try {
			PrintWriter out = this.getResponse().getWriter();
			if(result){
				out.print("have");
			}else{
				out.print("hasnot");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return NONE;
	}
}
